//
//  API_User.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit
import Moya

enum API_User {
    /// 用户余额
    case userBalance
    /// 查询签到
    case checkTicket
    /// 签到
    case ticket(Int, Int)
    /// 修改用户信息
    case changeUserInfo(String, String, Int, String)
    /// 上传头像
    case uploadAvatar(Data)
    /// 用户偏好
    case getPreference
    /// 保存用户偏好
    case savePreference(String)
    /// 获取最近阅读的一本书
    case getLastBook
}

let user_timeoutClosure = { (endpoint: Endpoint, done: @escaping MoyaProvider<API_User>.RequestResultClosure) in
    do {
        var request = try endpoint.urlRequest()
        request.timeoutInterval = 5
        done(.success(request))
    }catch {
        return
    }
}

let provider_user = MoyaProvider<API_User>(requestClosure: user_timeoutClosure)

extension API_User: TargetType {
    var baseURL: URL {
        return URL(string: server.APIHost)!
    }
    
    var path: String {
        switch self {
        case .userBalance:
            return "/Check/balance"
        case .checkTicket:
            return "/Check/check_in"
        case .ticket:
            return "/Check/check_in"
        case .changeUserInfo(_, _, _, _):
            return "/User/saveUserMessage"
        case .uploadAvatar(_):
            return "/User/uploads"
        case .getPreference:
            return "/User/getLabel"
        case .savePreference(_):
            return "/User/saveUserLabel"
        case .getLastBook:
            return "/User/getLastread"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .checkTicket:
            return .get
        default:
            return .post
        }
    }
    
    var parameterEncoding: ParameterEncoding {
        return JSONEncoding.default
    }
    
    var sampleData: Data {
        return "test".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        var ps: [String: Any] = ["ver": getAPPVersion(), "channel": "ios", "third_channel": "apple", "timestamp": Date().timeStamp, "userId": getUser()?.userId ?? "", "uuid": getKeyUUId(), "user_channel": getADID()]
        switch self {
        case .userBalance:
            break
        case .checkTicket:
            return .requestParameters(parameters: ps, encoding: URLEncoding.default)
        case .ticket(let day, let gold):
            ps["day"] = day
            ps["gold"] = gold
        case .changeUserInfo(let avatar, let realName, let sex, let birthday):
            ps["avatar"] = avatar
            ps["realName"] = realName
            ps["sex"] = sex
            ps["birthday"] = birthday
        case .uploadAvatar(let file):
            let formData = MultipartFormData(provider: .data(file), name: "file", fileName: "avatar.jpg", mimeType: "image/jpg")
            return .uploadCompositeMultipart([formData], urlParameters: ps)
        case .getPreference:
            break
        case .savePreference(let list):
            ps["list"] = list
        case .getLastBook:
            break
        }
        return .requestParameters(parameters: ps, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        switch self {
        default:
            return ["content-type": "application/json; charset=UTF-8"]
        }
    }
}
